package com.yc.SelectCourse.ui;

import org.eclipse.swt.widgets.Composite;

import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.SelectCourse.biz.BizException;
import com.yc.SelectCourse.dao.FindSelectedCourseDao;
import com.yc.SelectCourse.dao.SelectCourseDao;
import com.yc.SelectCourse.util.StuUtils;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class FindSelectedCourseUI extends Composite {
	private Table table;
	private FindSelectedCourseDao dao = new FindSelectedCourseDao();
	private Text text_id;
	private Text text_name;
	private Text text_fen;
	private Text text_teacher;
	private Label lable_page;
	protected int page = 1;//默认页数
	protected int size = 10;//默认分页大小
	protected int endPage;
	private SelectCourseDao selectDao = new SelectCourseDao();
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public FindSelectedCourseUI(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		setLayout(new FormLayout());
		
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		
		FormData fd_table = new FormData();
		fd_table.bottom = new FormAttachment(40, -30);
		fd_table.top = new FormAttachment(0, 21);
		fd_table.left = new FormAttachment(0, 10);
		fd_table.right = new FormAttachment(100, -10);
		table.setLayoutData(fd_table);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		
		TableColumn idTbl = new TableColumn(table, SWT.NONE);
		idTbl.setWidth(137);
		idTbl.setText("课程编号");
		
		TableColumn nameTbl = new TableColumn(table, SWT.NONE);
		nameTbl.setWidth(117);
		nameTbl.setText("课程名称");
		
		TableColumn creditTbl = new TableColumn(table, SWT.NONE);
		creditTbl.setWidth(109);
		creditTbl.setText("学分");
		
		TableColumn teacherTbl = new TableColumn(table, SWT.NONE);
		teacherTbl.setWidth(100);
		teacherTbl.setText("任课教师");
		
		TableColumn roomTbl = new TableColumn(table, SWT.NONE);
		roomTbl.setWidth(100);
		roomTbl.setText("教室");
		
		lable_page = new Label(this, SWT.NONE);
		lable_page.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lable_page.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		FormData fd_lable_page = new FormData();
		lable_page.setLayoutData(fd_lable_page);
		
		Button btn_shang = new Button(this, SWT.NONE);
		fd_lable_page.right = new FormAttachment(btn_shang, 94, SWT.RIGHT);
		fd_lable_page.left = new FormAttachment(btn_shang, 52);
		fd_lable_page.top = new FormAttachment(btn_shang, 5, SWT.TOP);
		btn_shang.setText("上一页");
		btn_shang.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		FormData fd_btn_shang = new FormData();
		fd_btn_shang.top = new FormAttachment(table, 59);
		fd_btn_shang.left = new FormAttachment(0, 112);
		btn_shang.setLayoutData(fd_btn_shang);
		
		Button btn_xia = new Button(this, SWT.CENTER);
		btn_xia.setText("下一页");
		btn_xia.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		FormData fd_btn_xia = new FormData();
		fd_btn_xia.top = new FormAttachment(lable_page, -5, SWT.TOP);
		fd_btn_xia.right = new FormAttachment(lable_page, 105, SWT.RIGHT);
		fd_btn_xia.left = new FormAttachment(lable_page, 33);
		btn_xia.setLayoutData(fd_btn_xia);
		
		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setText("课程编号");
		lblNewLabel.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		FormData fd_lblNewLabel = new FormData();
		lblNewLabel.setLayoutData(fd_lblNewLabel);
		
		text_id = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		fd_lblNewLabel.top = new FormAttachment(text_id, -1, SWT.TOP);
		fd_lblNewLabel.right = new FormAttachment(text_id, -22);
		FormData fd_text_id = new FormData();
		fd_text_id.top = new FormAttachment(btn_shang, 60);
		fd_text_id.left = new FormAttachment(0, 156);
		text_id.setLayoutData(fd_text_id);
		
		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		fd_text_id.right = new FormAttachment(lblNewLabel_1, -43);
		lblNewLabel_1.setText("课程名称");
		lblNewLabel_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		FormData fd_lblNewLabel_1 = new FormData();
		fd_lblNewLabel_1.top = new FormAttachment(btn_xia, 59);
		lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
		
		text_name = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		fd_lblNewLabel_1.right = new FormAttachment(text_name, -51);
		FormData fd_text_name = new FormData();
		fd_text_name.right = new FormAttachment(0, 506);
		fd_text_name.top = new FormAttachment(table, 150);
		fd_text_name.left = new FormAttachment(0, 410);
		text_name.setLayoutData(fd_text_name);
		
		Label lblNewLabel_2 = new Label(this, SWT.NONE);
		lblNewLabel_2.setText("课程学分");
		lblNewLabel_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		FormData fd_lblNewLabel_2 = new FormData();
		fd_lblNewLabel_2.top = new FormAttachment(lblNewLabel, 36);
		fd_lblNewLabel_2.right = new FormAttachment(lblNewLabel, 0, SWT.RIGHT);
		lblNewLabel_2.setLayoutData(fd_lblNewLabel_2);
		
		text_fen = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		FormData fd_text_fen = new FormData();
		fd_text_fen.top = new FormAttachment(lblNewLabel_2, 1, SWT.TOP);
		fd_text_fen.left = new FormAttachment(text_id, 0, SWT.LEFT);
		text_fen.setLayoutData(fd_text_fen);
		
		Label lblNewLabel_3 = new Label(this, SWT.NONE);
		fd_text_fen.right = new FormAttachment(lblNewLabel_3, -47);
		lblNewLabel_3.setText("任课教师");
		lblNewLabel_3.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		lblNewLabel_3.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
		FormData fd_lblNewLabel_3 = new FormData();
		fd_lblNewLabel_3.top = new FormAttachment(lblNewLabel_1, 36);
		fd_lblNewLabel_3.left = new FormAttachment(btn_xia, 0, SWT.LEFT);
		lblNewLabel_3.setLayoutData(fd_lblNewLabel_3);
		
		text_teacher = new Text(this, SWT.BORDER | SWT.READ_ONLY);
		FormData fd_text_teacher = new FormData();
		fd_text_teacher.right = new FormAttachment(lblNewLabel_3, 143, SWT.RIGHT);
		fd_text_teacher.top = new FormAttachment(text_name, 34);
		fd_text_teacher.left = new FormAttachment(lblNewLabel_3, 47);
		text_teacher.setLayoutData(fd_text_teacher);
		
		Button button = new Button(this, SWT.NONE);
		
		FormData fd_button = new FormData();
		fd_button.top = new FormAttachment(text_fen, 63);
		fd_button.left = new FormAttachment(0, 171);
		fd_button.right = new FormAttachment(0, 295);
		button.setLayoutData(fd_button);
		button.setText("取消选课");
		
		showCourse();
		
		try {
			int number = selectDao.findDateNum();
			if(StuUtils.checkNum(number, size)) {
				endPage=number/size;
			}else {
				endPage=number/size+1;
			}
		} catch (BizException e2) {
			e2.printStackTrace();
		}
		
		
		
		
		
		//
		
		//选中表格获取内容
		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem tableItem =(TableItem) e.item;
				text_id.setText(tableItem.getText(0));
				text_name.setText(tableItem.getText(1));
				text_fen.setText(tableItem.getText(2));
				text_teacher.setText(tableItem.getText(3));
			}
		});
		
		//下一页
		btn_xia.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				
				if(page<endPage) {
					page++;
				}
			showCourse();
			
			}
		});
		//上一页
		btn_shang.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(page>1) {
					page--;
				}else {
					return;
				}
				
				showCourse();
			}
		});
		
		//取消选课
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StuUtils.showMessage(getShell(), "确定取消选课?");
				try {
					dao.delete(text_id.getText().trim());
					StuUtils.showMessage(getShell(), "取消成功");
					showCourse();
				} catch (BizException e1) {
					e1.printStackTrace();
					StuUtils.showMessage(getShell(), "删除失败,"+e1.getMessage());
				}
			}
		});
		
		
		
		
		
		
		
		
		

	}
	
	public void showCourse() {
		try {
			//接收查询的数据
			List<Map<String,Object>> list = dao.findSelected("10011001001");//StuUtils.user.get("SID") 获取用户ID
			//清空表数据
			table.removeAll();
			lable_page.setText(StuUtils.objToString(page));
			TableItem tableItem = null;
			for (Map<String, Object> map : list) {
				
				tableItem = new TableItem(table, SWT.NONE);
				tableItem.setText(new String[] {
						StuUtils.objToString(map.get("ID")),
						StuUtils.objToString(map.get("COURSENAME")),
						StuUtils.objToString(map.get("CREADIT")),
						StuUtils.objToString(map.get("TEACHERNAME")),
						StuUtils.objToString(map.get("ROOM")),
				});
			}
		} catch (BizException e) {
			e.printStackTrace();
			StuUtils.showMessage(getShell(), e.getMessage());
		}
		
	}
	
	

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
